import { getAreaObj, uploadType } from '@/utils/util'
import { revertUTF8 } from '@/views/oneStopOffice/utils/index'
import { toSearchHandle, configToSearch, configToReserveSearch } from '@/views/datacenter/comprehensiveQueries/components/NewKeywords/components/utils/index'

export const BaseFormConfig = {
	inline: false,
	showBtn: false,
	labelPosition: 'top',
	column: [
		{
			label: '标题',
			prop: 'policyTitle',
			maxlength: '20',
			showWordLimit: true,
			type: 'text',
			changeField: true,
			span: 8,
		},
		{
			label: '发文字号',
			prop: 'policyArticleCode',
			type: 'text',
			changeField: true,
			span: 8,
		},
		{
			label: '关键词',
			prop: 'policyKeywords',
			maxlength: '20',
			showWordLimit: true,
			type: 'text',
			changeField: true,
			span: 8,
		},
		{
			label: '文章摘要',
			prop: 'infoAbstract',
			type: 'text',
			changeField: true,
			span: 8,
		},
		{
			label: '来源层级',
			prop: 'fromLevel',
			propName: 'fromLevelLabel',
			changeField: true,
			type: 'remoteSelect',
			code: 'POLICY_LEVEL',
			api: 'getDictItemList',
			filterKey: 'value',
			multiple: false,
			span: 8,
		},
		{
			label: '来源单位名称',
			prop: 'fromName',
			maxlength: '20',
			showWordLimit: true,
			type: 'text',
			changeField: true,
			span: 8,
		},
		{
			label: '来源单位区划',
			prop: 'fromAreaId',
			propName: 'from_area_id',
			type: 'AplidAreaSelect',
			changeField: true,
			span: 8,
		},
		{
			label: '养老业务标签',
			prop: 'policyTags',
			propName: 'policyTagsLabels',
			type: 'remoteSelect',
			multiple: false,
			code: 'POLICY_TAGS',
			filterKey: 'value',
			api: 'getDictItemList',
			changeField: true,
			span: 8,
			
		},
		{
			label: '发布日期',
			prop: 'publishDate',
			changeField: true,
			type: 'date',
			format: 'YYYY年MM月DD日',
			valueFormat: 'YYYY-MM-DD',
			span: 8,
		},
		// {
		// 	label: '适应老人年龄',
		// 	prop: 'ageRange',
		// 	changeField: true,
		// 	append: '岁',
		// 	isNum: true,
		// 	type: 'input',
		// 	span: 8,
			
		// },
		{
			label: '老人类别',
			prop: 'oldmanType',
			propName: 'oldmanTypeLabel',
			type: 'remoteSelect',
			code: 'ELDERLY_TYPE',
			api: 'getDictItemList',
			changeField: true,
			multiple: false,
			span: 8,
			
		},
		{
			label: '政策正文',
			prop: 'policyContent',
			type: 'text',
			changeField: true,
			span: 8,
		},
		{
			label: '附件',
			prop: 'policyAttachment',
			type: 'upload',
			fileSize:100,
			// apiName:"ost_uploadFile2",
			span: 24,
			changeField: true,
			fileType: uploadType.fileMetaType,
			listType: 'text',
		},
		// {
		// 	label:"发布状态",
		// 	prop: 'publishStatus',
		// 	propName: 'publishStatusLabel',
		// 	type: 'remoteSelect',
		// 	code: 'PUBLISH_STATUS',
		// 	api: 'getDictItemList',
		// 	span: 8,
		// 	rules: [
		// 		{
		// 			required: true,
		// 			message: '请选择发布状态',
		// 			trigger: 'blur',
		// 		}
		// 	]
		// }
	],
}

// 全字段搜索
const moreSearchConfig: any = {
	inline: true,
	showBtn: false,
	labelPosition: 'left',
	column: [...configToSearch(BaseFormConfig.column)],
}
const reserveSearchConfig: any = {
	inline: true,
	showBtn: false,
	labelPosition: 'left',
	column: [...configToReserveSearch(BaseFormConfig.column)],
}

export const baseConfig: any = {
	dictcodes: ['PUBLISH_STATUS','BLOCK_CHAIN_STATUS'],
	api: {
		moreSearch: 'search_policyDocument',
		reserveSearch: 'view_dpProfessor_search',
	},
	dialogConfig: {
		width: '65%',
	},
	
	beforeOnLoadFormate: async (query) => {
		if (query.publishDateBetween) {
			query.publishDateBetween = query.publishDateBetween.join(',')
		}
		if(query.adaptiveHierarchy && Array.isArray(query.adaptiveHierarchy)){
			if(query.adaptiveHierarchy.length==1){
				query.adaptiveHierarchyCity = query.adaptiveHierarchy[0]
				query.adaptiveHierarchyArea = undefined
			}else{
				query.adaptiveHierarchyCity = query.adaptiveHierarchy[0]
				query.adaptiveHierarchyArea = query.adaptiveHierarchy[1]
			}
			query.adaptiveHierarchy = undefined
		}
		return new Promise((resolve, reject) => {
			const crAreasObj: any = getAreaObj(query.authAreas, '')
			resolve({
				...query,
				...crAreasObj,
				authAreas: undefined,
			})
		})
	},
	onLoadFormate: async (arr) => {
		const tableData = arr.map((item) => {
			let tempAge = null
			if(item.ageRange){
				tempAge = item.ageRange.split(',')
				item.age = `${tempAge[0]} - ${tempAge[1]}`
			}
			if(item.policyKeywords){
				item.policyKeywords = item.policyKeywords.split(',')
			}
			if(item.policyContent){
				item.policyContent = revertUTF8(item.policyContent) 
			}
			if(item.adaptiveHierarchy){
				item.adaptiveHierarchy = item.adaptiveHierarchy.split(",")
				item.adaptiveHierarchy = item.adaptiveHierarchy.map(Number)
			}
			
			let fromAreaObj:any = getAreaObj(item,'from','set')
			return {
				...item,
				areas:fromAreaObj.areaIds
			}
		})
		return Promise.resolve(Object.freeze(tableData))
	},
	formConfig: {
		...BaseFormConfig,
		showBtn: true,
	},
	moreSearchConfig,
	reserveSearchConfig,
}
